package com.example.elmmerchants.Product_Management;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;

public class Shop_Management_Page extends AppCompatActivity {

    TextView all_product_number, sold_product_number, do_not_sold_product_number, do_not_buy_product_number;
    LinearLayout set_category_layout, product_list_layout, category_management, add_product;
    ScrollView product_list_view;
    CardView last_view;
    View last_left_view;
    LinearLayout show_all, just_show_sold, just_show_not_buy, just_show_not_sold, sort;
    View show_all_view, just_show_sold_view, just_show_not_buy_view, just_show_not_sold_view;
    View last_show_view;
    EditText search_product;

    String email, category_text;
    int all, sold, do_not_sold, do_not_buy;
    ArrayList<String> category = new ArrayList<>();
    List<List<String>> product_data = new ArrayList<>();
    List<byte[]> product_pic = new ArrayList<>();
    boolean is_loading_data = false, is_update_price_and_inventory = false, is_loading_product_data = false;
    AlertDialog dialog;
    String show_what = "全部";
    String open_hour = "", close_hour = "", open_hour_2 = "", close_hour_2 = "", sold_day = "";

    private class Product_Data_Thread extends Thread {

        String category;
        String show_what;

        public Product_Data_Thread(String category, String show_what) {
            this.category = category;
            this.show_what = show_what;
        }

        @Override
        public void run() {
            is_loading_product_data = false;
            product_pic.clear();
            product_data.clear();

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();

                    PreparedStatement preparedStatement2 = null;
                    if (category.equals("全部") && show_what.equals("全部")) {
                        String sql2 = "SELECT * FROM `" + email + "_merchant_product`;";
                        preparedStatement2 = connection.prepareStatement(sql2);
                    }
                    else if (!category.equals("全部") && show_what.equals("全部")) {
                        String sql2 = "SELECT * FROM `" + email + "_merchant_product` WHERE category_1 = ? OR category_2 = ? OR category_3 = ? OR category_4 = ? OR category_5 = ?;";
                        preparedStatement2 = connection.prepareStatement(sql2);
                        preparedStatement2.setString(1, category);
                        preparedStatement2.setString(2, category);
                        preparedStatement2.setString(3, category);
                        preparedStatement2.setString(4, category);
                        preparedStatement2.setString(5, category);
                    }
                    else if (category.equals("全部") && show_what.equals("售卖中")) {
                        String sql2 = "SELECT * FROM `" + email + "_merchant_product` WHERE product_state = ?;";
                        preparedStatement2 = connection.prepareStatement(sql2);
                        preparedStatement2.setInt(1, 0);
                    }
                    else if (!category.equals("全部") && show_what.equals("售卖中")) {
                        String sql2 = "SELECT * FROM `" + email + "_merchant_product` WHERE (category_1 = ? OR category_2 = ? OR category_3 = ? OR category_4 = ? OR category_5 = ?) AND product_state = ?;";
                        preparedStatement2 = connection.prepareStatement(sql2);
                        preparedStatement2.setString(1, category);
                        preparedStatement2.setString(2, category);
                        preparedStatement2.setString(3, category);
                        preparedStatement2.setString(4, category);
                        preparedStatement2.setString(5, category);
                        preparedStatement2.setInt(6, 0);
                    }
                    else if (category.equals("全部") && show_what.equals("已下架")) {
                        String sql2 = "SELECT * FROM `" + email + "_merchant_product` WHERE product_state = ?;";
                        preparedStatement2 = connection.prepareStatement(sql2);
                        preparedStatement2.setInt(1, 1);
                    }
                    else if (!category.equals("全部") && show_what.equals("已下架")) {
                        String sql2 = "SELECT * FROM `" + email + "_merchant_product` WHERE (category_1 = ? OR category_2 = ? OR category_3 = ? OR category_4 = ? OR category_5 = ?) AND product_state = ?;";
                        preparedStatement2 = connection.prepareStatement(sql2);
                        preparedStatement2.setString(1, category);
                        preparedStatement2.setString(2, category);
                        preparedStatement2.setString(3, category);
                        preparedStatement2.setString(4, category);
                        preparedStatement2.setString(5, category);
                        preparedStatement2.setInt(6, 1);
                    }
                    else if (category.equals("全部") && show_what.equals("已售罄")) {
                        String sql2 = "SELECT * FROM `" + email + "_merchant_product` WHERE product_state = ?;";
                        preparedStatement2 = connection.prepareStatement(sql2);
                        preparedStatement2.setInt(1, 2);
                    }
                    else if (!category.equals("全部") && show_what.equals("已售罄")) {
                        String sql2 = "SELECT * FROM `" + email + "_merchant_product` WHERE (category_1 = ? OR category_2 = ? OR category_3 = ? OR category_4 = ? OR category_5 = ?) AND product_state = ?;";
                        preparedStatement2 = connection.prepareStatement(sql2);
                        preparedStatement2.setString(1, category);
                        preparedStatement2.setString(2, category);
                        preparedStatement2.setString(3, category);
                        preparedStatement2.setString(4, category);
                        preparedStatement2.setString(5, category);
                        preparedStatement2.setInt(6, 2);
                    }
                    ResultSet set2 = preparedStatement2.executeQuery();
                    while (set2.next()) {
                        List<String> product = new ArrayList<>();
                        product.add(String.valueOf(set2.getInt("inventory")));
                        product.add(set2.getString("name"));
                        product.add(set2.getString("category_1"));
                        product.add(set2.getString("category_2"));
                        product.add(set2.getString("category_3"));
                        product.add(set2.getString("category_4"));
                        product.add(set2.getString("category_5"));
                        product.add(String.valueOf(set2.getInt("sold")));
                        product_pic.add(set2.getBytes("picture"));
                        product.add(String.valueOf(set2.getInt("is_single_order_without_delivery")));
                        product.add(String.valueOf(set2.getDouble("product_price")));
                        product.add(String.valueOf(set2.getInt("is_auto_return_inventory")));
                        product.add(set2.getString("description"));
                        product.add(set2.getString("barcode"));
                        product.add(String.valueOf(set2.getInt("product_state")));
                        product.add(set2.getString("specification_name"));
                        product.add(set2.getString("specification_data"));
                        product.add(set2.getString("attributes_name"));
                        product.add(set2.getString("attributes_label"));
                        product.add(String.valueOf(set2.getInt("weight")));
                        product.add(String.valueOf(set2.getInt("min_purchase")));

                        product_data.add(product);
                    }
                    is_loading_product_data = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_loading_product_data) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    product_list_layout.removeAllViews();
                    for (int i = 0; i < product_data.size(); i++) {
                        View view = LayoutInflater.from(Shop_Management_Page.this).inflate(R.layout.product_view, null);

                        CardView cardView = view.findViewById(R.id.card_view);

                        RelativeLayout not_sold_image = view.findViewById(R.id.not_sold_image);

                        ImageView product_pic_view = view.findViewById(R.id.product_pic);
                        Bitmap decodedImage = BitmapFactory.decodeByteArray(product_pic.get(i), 0, product_pic.get(i).length);
                        product_pic_view.setImageBitmap(decodedImage);

                        TextView product_shelves_text_view = view.findViewById(R.id.product_shelves_text_view);

                        TextView product_name = view.findViewById(R.id.product_name);
                        product_name.setText(product_data.get(i).get(1));

                        TextView product_inventory = view.findViewById(R.id.product_inventory);
                        product_inventory.setText(product_data.get(i).get(0));

                        TextView product_sold = view.findViewById(R.id.product_sold);
                        product_sold.setText(product_data.get(i).get(7));

                        TextView product_price = view.findViewById(R.id.product_price);
                        product_price.setText(product_data.get(i).get(9));

                        TextView dollars = view.findViewById(R.id.dollars);

                        TextView product_state = view.findViewById(R.id.product_state);

                        if (product_data.get(i).get(13).equals("1")) {
                            product_shelves_text_view.setText("上架");
                            cardView.setBackgroundColor(Color.rgb(245, 246, 251));
                            product_name.setTextColor(Color.rgb(128, 128, 128));
                            dollars.setTextColor(Color.rgb(128, 128, 128));
                            product_price.setTextColor(Color.rgb(128, 128, 128));
                            not_sold_image.setVisibility(View.VISIBLE);
                            product_state.setText("暂停售卖");
                        }
                        else if (product_inventory.getText().toString().equals("0")) {
                            cardView.setBackgroundColor(Color.rgb(245, 246, 251));
                            product_name.setTextColor(Color.rgb(128, 128, 128));
                            dollars.setTextColor(Color.rgb(128, 128, 128));
                            product_price.setTextColor(Color.rgb(128, 128, 128));
                            not_sold_image.setVisibility(View.VISIBLE);
                            product_state.setText("暂无库存");
                        }

                        LinearLayout update_price_and_inventory = view.findViewById(R.id.update_price_and_inventory);
                        int finalI = i;
                        update_price_and_inventory.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (product_shelves_text_view.getText().toString().equals("下架")) {
                                    Dialog dialog1 = new Dialog(view.getContext());
                                    dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
                                    dialog1.setContentView(R.layout.update_price_and_inventory_view);

                                    TextView cancel = dialog1.findViewById(R.id.cancel);
                                    cancel.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            dialog1.dismiss();
                                        }
                                    });
                                    TextView product_weight = dialog1.findViewById(R.id.product_weight);
                                    product_weight.setText(product_data.get(finalI).get(18) + "克");
                                    EditText update_product_price = dialog1.findViewById(R.id.product_price);
                                    update_product_price.setText(product_data.get(finalI).get(9));
                                    EditText update_product_inventory = dialog1.findViewById(R.id.product_inventory);
                                    update_product_inventory.setText(product_data.get(finalI).get(0));
                                    Switch is_next_day_return_inventory = dialog1.findViewById(R.id.is_next_day_return_inventory);
                                    if (product_data.get(finalI).get(10).equals("0")) {
                                        is_next_day_return_inventory.setChecked(false);
                                    }
                                    else {
                                        is_next_day_return_inventory.setChecked(true);
                                    }
                                    LinearLayout get_zero_inventory = dialog1.findViewById(R.id.get_zero_inventory);
                                    get_zero_inventory.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            update_product_inventory.setText("0");
                                        }
                                    });
                                    TextView update = dialog1.findViewById(R.id.update);
                                    update.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            is_update_price_and_inventory = false;
                                            ExecutorService executorService1 = Executors.newSingleThreadExecutor();
                                            executorService1.execute(() -> {
                                                try {
                                                    Connection_SQL userSQL = new Connection_SQL();
                                                    Connection connection = userSQL.connection();
                                                    String sql3 = "update `elm`.`" + email + "_merchant_product` set product_price = ?, inventory = ?, is_auto_return_inventory = ? where name = ?";
                                                    PreparedStatement preparedStatement2 = connection.prepareStatement(sql3);
                                                    preparedStatement2.setDouble(1, Double.parseDouble(update_product_price.getText().toString()));
                                                    preparedStatement2.setInt(2, Integer.parseInt(update_product_inventory.getText().toString()));
                                                    if (is_next_day_return_inventory.isChecked()) {
                                                        preparedStatement2.setInt(3, 1);
                                                    }
                                                    else {
                                                        preparedStatement2.setInt(3, 0);
                                                    }
                                                    preparedStatement2.setString(4, product_name.getText().toString());
                                                    preparedStatement2.executeUpdate();
                                                    is_update_price_and_inventory = true;
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                            });

                                            while (!is_update_price_and_inventory) {
                                                try {
                                                    Thread.sleep(1);
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                            }

                                            product_price.setText(update_product_price.getText().toString());
                                            product_inventory.setText(update_product_inventory.getText().toString());
                                            product_data.get(finalI).set(9, update_product_price.getText().toString());
                                            product_data.get(finalI).set(0, update_product_inventory.getText().toString());
                                            if (is_next_day_return_inventory.isChecked()) {
                                                product_data.get(finalI).set(10, "1");
                                            }
                                            else {
                                                product_data.get(finalI).set(10, "0");
                                            }
                                            if (update_product_inventory.getText().toString().equals("0")) {
                                                cardView.setBackgroundColor(Color.rgb(245, 246, 251));
                                                product_name.setTextColor(Color.rgb(128, 128, 128));
                                                dollars.setTextColor(Color.rgb(128, 128, 128));
                                                product_price.setTextColor(Color.rgb(128, 128, 128));
                                                not_sold_image.setVisibility(View.VISIBLE);
                                                product_state.setText("暂无库存");
                                                sold -= 1;
                                                do_not_buy += 1;
                                                sold_product_number.setText(String.valueOf(sold));
                                                do_not_buy_product_number.setText(String.valueOf(do_not_buy));
                                            }
                                            else {
                                                cardView.setBackgroundColor(Color.WHITE);
                                                product_name.setTextColor(Color.BLACK);
                                                dollars.setTextColor(Color.RED);
                                                product_price.setTextColor(Color.RED);
                                                not_sold_image.setVisibility(View.GONE);
                                                sold += 1;
                                                do_not_buy -= 1;
                                                sold_product_number.setText(String.valueOf(sold));
                                                do_not_buy_product_number.setText(String.valueOf(do_not_buy));
                                            }
                                            dialog1.dismiss();
                                        }
                                    });

                                    dialog1.show();
                                    dialog1.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                                    dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                                    dialog1.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                                    dialog1.getWindow().setGravity(Gravity.BOTTOM);
                                }
                            }
                        });

                        LinearLayout is_taken_off_the_shelves = view.findViewById(R.id.is_taken_off_the_shelves);
                        is_taken_off_the_shelves.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (!product_inventory.getText().toString().equals("0")) {
                                    if (product_shelves_text_view.getText().toString().equals("下架")) {
                                        ExecutorService executorService1 = Executors.newSingleThreadExecutor();
                                        executorService1.execute(() -> {
                                            try {
                                                Connection_SQL userSQL = new Connection_SQL();
                                                Connection connection = userSQL.connection();
                                                String sql3 = "update `elm`.`" + email + "_merchant_product` set product_state = ? where name = ?";
                                                PreparedStatement preparedStatement2 = connection.prepareStatement(sql3);
                                                preparedStatement2.setInt(1, 1);
                                                preparedStatement2.setString(2, product_name.getText().toString());
                                                preparedStatement2.executeUpdate();
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }

                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    try {
                                                        Thread.sleep(100);
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                    }

                                                    product_shelves_text_view.setText("上架");
                                                    cardView.setBackgroundColor(Color.rgb(245, 246, 251));
                                                    product_name.setTextColor(Color.rgb(128, 128, 128));
                                                    dollars.setTextColor(Color.rgb(128, 128, 128));
                                                    product_price.setTextColor(Color.rgb(128, 128, 128));
                                                    not_sold_image.setVisibility(View.VISIBLE);
                                                    sold -= 1;
                                                    do_not_sold += 1;
                                                    sold_product_number.setText(String.valueOf(sold));
                                                    do_not_sold_product_number.setText(String.valueOf(do_not_sold));
                                                }
                                            });
                                        });
                                    }
                                    else {
                                        ExecutorService executorService1 = Executors.newSingleThreadExecutor();
                                        executorService1.execute(() -> {
                                            try {
                                                Connection_SQL userSQL = new Connection_SQL();
                                                Connection connection = userSQL.connection();
                                                String sql3 = "update `elm`.`" + email + "_merchant_product` set product_state = ? where name = ?";
                                                PreparedStatement preparedStatement2 = connection.prepareStatement(sql3);
                                                preparedStatement2.setInt(1, 0);
                                                preparedStatement2.setString(2, product_name.getText().toString());
                                                preparedStatement2.executeUpdate();
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }

                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    try {
                                                        Thread.sleep(100);
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                    }

                                                    product_shelves_text_view.setText("下架");
                                                    cardView.setBackgroundColor(Color.WHITE);
                                                    product_name.setTextColor(Color.BLACK);
                                                    dollars.setTextColor(Color.RED);
                                                    product_price.setTextColor(Color.RED);
                                                    not_sold_image.setVisibility(View.GONE);
                                                    sold += 1;
                                                    do_not_sold -= 1;
                                                    sold_product_number.setText(String.valueOf(sold));
                                                    do_not_sold_product_number.setText(String.valueOf(do_not_sold));
                                                }
                                            });
                                        });
                                    }
                                }
                            }
                        });

                        LinearLayout update_product_data = view.findViewById(R.id.update_product_data);
                        update_product_data.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                String category = "";
                                for (int i = 2; i <= 6; i++) {
                                    if (!product_data.get(finalI).get(i).isEmpty()) {
                                        category += product_data.get(finalI).get(i);
                                        category += ",";
                                    }
                                }

                                Intent intent = new Intent(Shop_Management_Page.this, Update_Product_Data_Page.class);
                                intent.putExtra("product_name", product_data.get(finalI).get(1));
                                intent.putExtra("product_category", category);
                                intent.putExtra("specification_name", product_data.get(finalI).get(14));
                                intent.putExtra("specification_data", product_data.get(finalI).get(15));
                                intent.putExtra("barcode", product_data.get(finalI).get(12));
                                intent.putExtra("product_price", product_data.get(finalI).get(9));
                                intent.putExtra("product_inventory", product_data.get(finalI).get(0));
                                intent.putExtra("is_auto_return_inventory", product_data.get(finalI).get(10));
                                intent.putExtra("product_weight", product_data.get(finalI).get(18));
                                intent.putExtra("attributes_name", product_data.get(finalI).get(16));
                                intent.putExtra("attributes_label", product_data.get(finalI).get(17));
                                intent.putExtra("email", email);
                                intent.putExtra("category_text", category_text);
                                intent.putExtra("is_single_order_without_delivery", product_data.get(finalI).get(8));
                                intent.putExtra("min_purchase", product_data.get(finalI).get(19));
                                startActivityForResult(intent, 1);
                            }
                        });

                        product_list_layout.addView(view);
                    }
                }
            });
        }
    }

    private class Data_Thread extends Thread {

        String show_what;

        public Data_Thread(String show_what) {
            this.show_what = show_what;
        }

        @Override
        public void run() {
            is_loading_data = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "SELECT * FROM merchant WHERE email = ?;";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, email);
                    ResultSet set = preparedStatement.executeQuery();
                    while (set.next()) {
                        open_hour = set.getString("open_hour");
                        close_hour = set.getString("close_hour");
                        open_hour_2 = set.getString("open_hour2");
                        close_hour_2 = set.getString("close_hour2");
                        sold_day = set.getString("open_day");
                        category_text = set.getString("shop_category");
                        String category_temp = "";
                        for (int i = 0; i < set.getString("shop_category").length(); i++) {
                            if (set.getString("shop_category").charAt(i) != ',') {
                                category_temp += set.getString("shop_category").charAt(i);
                            }
                            else {
                                category.add(category_temp);
                                category_temp = "";
                            }
                        }
                    }

                    String sql2 = "SELECT * FROM `" + email + "_merchant_product`;";
                    PreparedStatement preparedStatement2 = connection.prepareStatement(sql2);
                    ResultSet set2 = preparedStatement2.executeQuery();
                    while (set2.next()) {
                        if (set2.getInt("product_state") == 0) {
                            sold += 1;
                        }
                        else if (set2.getInt("product_state") == 1) {
                            do_not_sold += 1;
                        }
                        if (set2.getInt("inventory") == 0) {
                            do_not_buy += 1;
                        }
                        all += 1;
                    }
                    is_loading_data = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_loading_data) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    all_product_number.setText(String.valueOf(all));
                    sold_product_number.setText(String.valueOf(sold));
                    do_not_sold_product_number.setText(String.valueOf(do_not_sold));
                    do_not_buy_product_number.setText(String.valueOf(do_not_buy));
                    set_category_layout.removeAllViews();
                    for (int i = 0; i < category.size(); i++) {
                        View view = LayoutInflater.from(Shop_Management_Page.this).inflate(R.layout.category_item_view, null);
                        CardView item = view.findViewById(R.id.item);
                        View left_view = view.findViewById(R.id.left_view);
                        TextView item_name = view.findViewById(R.id.item_name);
                        item_name.setText(category.get(i));
                        item_name.setPadding(0, 30, 0, 30);
                        if (i == 0) {
                            Product_Data_Thread productDataThread = new Product_Data_Thread("全部", show_what);
                            productDataThread.start();
                            item.setBackgroundColor(Color.rgb(245, 246, 251));
                            left_view.setVisibility(View.VISIBLE);
                            last_view = item;
                            last_left_view = left_view;
                        }
                        int finalI = i;
                        item.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                product_list_layout.removeAllViews();
                                product_list_layout.addView(LayoutInflater.from(Shop_Management_Page.this).inflate(R.layout.load_data, null));
                                Product_Data_Thread productDataThread = new Product_Data_Thread(category.get(finalI), show_what);
                                productDataThread.start();
                                if (last_view != item) {
                                    item.setBackgroundColor(Color.rgb(245, 246, 251));
                                    left_view.setVisibility(View.VISIBLE);
                                    last_view.setBackgroundColor(Color.WHITE);
                                    last_left_view.setVisibility(View.GONE);
                                    last_view = item;
                                    last_left_view = left_view;
                                }
                            }
                        });
                        set_category_layout.addView(view);
                    }
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_management_page);

        email = getIntent().getStringExtra("email");

        all_product_number = findViewById(R.id.all_product_number);
        sold_product_number = findViewById(R.id.sold_product_number);
        do_not_sold_product_number = findViewById(R.id.do_not_sold_product_number);
        do_not_buy_product_number = findViewById(R.id.do_not_buy_product_number);
        set_category_layout = findViewById(R.id.set_category_layout);
        product_list_view = findViewById(R.id.product_list_view);
        product_list_layout = findViewById(R.id.product_list_layout);
        category_management = findViewById(R.id.category_management);
        add_product = findViewById(R.id.add_product);
        show_all = findViewById(R.id.show_all);
        show_all_view = findViewById(R.id.show_all_view);
        just_show_sold = findViewById(R.id.just_show_sold);
        just_show_sold_view = findViewById(R.id.just_show_sold_view);
        just_show_not_buy = findViewById(R.id.just_show_not_buy);
        just_show_not_buy_view = findViewById(R.id.just_show_not_buy_view);
        just_show_not_sold = findViewById(R.id.just_show_not_sold);
        just_show_not_sold_view = findViewById(R.id.just_show_not_sold_view);
        search_product = findViewById(R.id.search_product);
        sort = findViewById(R.id.sort);

        category_management.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Shop_Management_Page.this, Category_Management_Page.class);
                intent.putExtra("email", email);
                intent.putExtra("category", category_text);
                intent.putExtra("open_hour", open_hour);
                intent.putExtra("close_hour", close_hour);
                intent.putExtra("open_hour_2", open_hour_2);
                intent.putExtra("close_hour_2", close_hour_2);
                intent.putExtra("sold_day", sold_day);
                startActivityForResult(intent, 1);
            }
        });

        add_product.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Dialog dialog1 = new Dialog(view.getContext());
                dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog1.setContentView(R.layout.add_product_view);
                LinearLayout create_product = dialog1.findViewById(R.id.create_product);
                create_product.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Shop_Management_Page.this, Create_Product_Page.class);
                        intent.putExtra("email", email);
                        intent.putExtra("category", category_text);
                        startActivityForResult(intent, 1);
                    }
                });

                dialog1.show();
                dialog1.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog1.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                dialog1.getWindow().setGravity(Gravity.BOTTOM);
            }
        });

        show_category(show_what);
        last_show_view = show_all_view;

        show_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show_what = "全部";
                if (last_show_view != show_all_view) {
                    show_all_view.setVisibility(View.VISIBLE);
                    last_show_view.setVisibility(View.GONE);
                    last_show_view = show_all_view;
                }
                show_category(show_what);
            }
        });

        just_show_sold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show_what = "售卖中";
                if (last_show_view != just_show_sold_view) {
                    just_show_sold_view.setVisibility(View.VISIBLE);
                    last_show_view.setVisibility(View.GONE);
                    last_show_view = just_show_sold_view;
                }
                show_category(show_what);
            }
        });

        just_show_not_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show_what = "已下架";
                if (last_show_view != just_show_not_buy_view) {
                    just_show_not_buy_view.setVisibility(View.VISIBLE);
                    last_show_view.setVisibility(View.GONE);
                    last_show_view = just_show_not_buy_view;
                }
                show_category(show_what);
            }
        });

        just_show_not_sold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show_what = "已售罄";
                if (last_show_view != just_show_not_sold_view) {
                    just_show_not_sold_view.setVisibility(View.VISIBLE);
                    last_show_view.setVisibility(View.GONE);
                    last_show_view = just_show_not_sold_view;
                }
                show_category(show_what);
            }
        });

        sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Shop_Management_Page.this, Sort_Page.class);
                intent.putExtra("email", email);
                startActivityForResult(intent, 1);
            }
        });
    }

    void show_category(String show_what) {
        set_category_layout.addView(LayoutInflater.from(Shop_Management_Page.this).inflate(R.layout.load_data, null));
        product_list_layout.addView(LayoutInflater.from(Shop_Management_Page.this).inflate(R.layout.load_data, null));
        is_loading_data = false;
        product_data.clear();
        product_pic.clear();
        category.clear();
        all = 0;
        sold = 0;
        do_not_sold = 0;
        do_not_buy = 0;

        Data_Thread dataThread = new Data_Thread(show_what);
        dataThread.start();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case 1: {
                product_pic.clear();
                category.clear();
                product_data.clear();
                product_list_layout.removeAllViews();
                set_category_layout.removeAllViews();
                show_all_view.setVisibility(View.VISIBLE);
                if (last_show_view != show_all_view) {
                    last_show_view.setVisibility(View.GONE);
                }
                last_show_view = show_all_view;
                show_category("全部");
            }
        }
    }
}